package com.cnswhy.cloud.server.merchant.controller.customer;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.core.utils.SystemClockUtil;
import com.cnswhy.cloud.common.logging.uitls.MDCUtil;
import com.cnswhy.cloud.common.rabbitmq.service.MessageService;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.enumeration.TransType;
import com.cnswhy.cloud.server.common.entity.enumeration.WalletType;
import com.cnswhy.cloud.server.common.entity.mq.WithdrawQueryMQ;
import com.cnswhy.cloud.server.common.entity.mq.WithdrawRecordMQ;
import com.cnswhy.cloud.server.common.entity.mq.WithdrawVerifyMQ;
import com.cnswhy.cloud.server.core.dto.CustomerWithdrawalRecordPageDTO;
import com.cnswhy.cloud.server.core.dto.CustomerWithdrawalRecordSaveDTO;
import com.cnswhy.cloud.server.core.dto.CustomerWithdrawalRecordUpdateDTO;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.CustomerWithdrawalRecord;
import com.cnswhy.cloud.server.core.entity.WithdrawalRecord;
import com.cnswhy.cloud.server.core.service.CustomerWithdrawalRecordService;
import com.cnswhy.cloud.server.core.service.trans.WithdrawalRecordService;
import com.cnswhy.cloud.server.core.wallet.WalletService;
import com.cnswhy.cloud.server.merchant.dto.CustomerWithdrawalVerifyUpdateDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-14
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/customerWithdrawalRecord")
@Api(value = "CustomerWithdrawalRecord", tags = "")
//@PreAuth(replace = "customerWithdrawalRecord:")
public class CustomerWithdrawalRecordController extends SuperController<CustomerWithdrawalRecordService, Long, CustomerWithdrawalRecord, CustomerWithdrawalRecordPageDTO, CustomerWithdrawalRecordSaveDTO, CustomerWithdrawalRecordUpdateDTO> {
    @Autowired
    WithdrawalRecordService withdrawalRecordService;
    @Autowired
    MessageService messageService;
    @ApiOperation(value = "发送消息队列", notes = "发送消息队列")
    @GetMapping("/sendMQ/{id}")
    public R<WithdrawalRecord> queryByRoleId(@PathVariable Long id) {
        WithdrawalRecord withdrawalRecord = withdrawalRecordService.getById(id);
        try {
            WithdrawRecordMQ withdrawRecordMQ = WithdrawRecordMQ.builder().id(withdrawalRecord.getId()).build();
            withdrawRecordMQ.setTraceId(MDCUtil.getTraceId());
            messageService.sendQueueByObject(AmqpExchange.CREATE_WITHDRAW_MESSAGE, withdrawRecordMQ);
        } catch (IllegalArgumentException e) {
            log.info("mq error");
        }
        return R.success(withdrawalRecord);
    }
    @ApiOperation(value = "发送消息查询队列", notes = "发送消息查询队列")
    @GetMapping("/sendQuery/{id}")
    public R<CustomerWithdrawalRecord> sendQuery(@PathVariable Long id) {
        CustomerWithdrawalRecord customerWithdrawalRecord = baseService.getById(id);
        try {
            WithdrawQueryMQ withdrawQueryMQ = WithdrawQueryMQ.builder().id(customerWithdrawalRecord.getId()).queryTimes(0L).build();
            withdrawQueryMQ.setTraceId(MDCUtil.getTraceId());
            messageService.sendAndQueryTimes(AmqpExchange.WITHDRAW_QUERY_MESSAGE, withdrawQueryMQ, withdrawQueryMQ.getQueryTimes());
        } catch (IllegalArgumentException e) {
            log.info("mq error");
        }
        return R.success(customerWithdrawalRecord);
    }
    @ApiOperation(value = "审核通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids[]", value = "主键id", dataType = "array", paramType = "query"),
            @ApiImplicitParam(name = "verifyStatus", value = "2审核通过 3审核拒绝", dataType = "long", paramType = "query"),
    })
    @PostMapping("/verifyStatus")
    //public R<Boolean> verifyStatus(@RequestParam("ids[]") List<String> ids,@RequestParam("verifyStatus") int verifyStatus) {
    public R<Boolean> verifyStatus(@RequestBody CustomerWithdrawalVerifyUpdateDTO customerWithdrawalVerifyUpdateDTO) {
        log.info(JSON.toJSONString(customerWithdrawalVerifyUpdateDTO));
        List<CustomerWithdrawalRecord> customerWithdrawalRecords = baseService.listByIds(customerWithdrawalVerifyUpdateDTO.getIds());
        for(var var : customerWithdrawalRecords){
            if(var.getVerifyStatus() != 1){
                return fail("存在已审核记录，请检查提现申请");
            }
            var.setVerifyStatus(customerWithdrawalVerifyUpdateDTO.getVerifyStatus());
            var.setVerifyTime(SystemClockUtil.getLocalDateTime());
            baseService.updateById(var);
            WithdrawVerifyMQ withdrawVerifyMQ = WithdrawVerifyMQ.builder().id(var.getId()).build();
            withdrawVerifyMQ.setTraceId(MDCUtil.getTraceId());
            messageService.sendQueueByObject(AmqpExchange.WITHDRAW_VERIFY_MESSAGE, withdrawVerifyMQ);


        }
        return success();
    }
    @Autowired
    WalletService walletService;
    @PostMapping("/repair")
    //public R<Boolean> verifyStatus(@RequestParam("ids[]") List<String> ids,@RequestParam("verifyStatus") int verifyStatus) {
    public R<Boolean> repair(String settDate) {
        /*List<CustomerWithdrawalRecord> customerWithdrawalRecords = baseService.list(Wraps.<CustomerWithdrawalRecord>lbQ().eq(CustomerWithdrawalRecord::getSettDate,settDate).eq(CustomerWithdrawalRecord::getStatus, 2));
        for(CustomerWithdrawalRecord customerWithdrawalRecord : customerWithdrawalRecords){
            walletService.ded(customerWithdrawalRecord.getCustomerId(), WalletType.BALANCE, TransType.WITHDRAW, customerWithdrawalRecord.getSettAmt(), customerWithdrawalRecord.getOrderId());

        }*/
        /*log.info(JSON.toJSONString(customerWithdrawalVerifyUpdateDTO));
        List<CustomerWithdrawalRecord> customerWithdrawalRecords = baseService.listByIds(customerWithdrawalVerifyUpdateDTO.getIds());
        for(var var : customerWithdrawalRecords){
            if(var.getVerifyStatus() != 1){
                return fail("存在已审核记录，请检查提现申请");
            }
            var.setVerifyStatus(customerWithdrawalVerifyUpdateDTO.getVerifyStatus());
            var.setVerifyTime(SystemClockUtil.getLocalDateTime());
            baseService.updateById(var);
            WithdrawVerifyMQ withdrawVerifyMQ = WithdrawVerifyMQ.builder().id(var.getId()).build();
            withdrawVerifyMQ.setTraceId(MDCUtil.getTraceId());
            messageService.sendQueueByObject(AmqpExchange.WITHDRAW_VERIFY_MESSAGE, withdrawVerifyMQ);


        }*/
        return success();
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<CustomerWithdrawalRecord> customerWithdrawalRecordList = list.stream().map((map) -> {
            CustomerWithdrawalRecord customerWithdrawalRecord = CustomerWithdrawalRecord.builder().build();
            return customerWithdrawalRecord;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(customerWithdrawalRecordList));
    }

}
